package cn.school;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class AddSubject {
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动没有找到!");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请分别输入课程名,学时还有年级号:");
		String sname = in.next();
		int hours = in.nextInt();
		long gid = in.nextLong();
		String url = "jdbc:mysql://localhost:3306/java67?useUnicode=true&characterEncoding=UTF8&useServerPrepStmts=true&prepStmtCacheSqlLimit=256&cachePrepStmts=true&prepStmtCacheSize=256&rewriteBatchedStatements=true&useSSL=false";
		Connection con = null;
		Statement stmt = null;
		try {
			con = DriverManager.getConnection(url, "zhaoyang", "zhaoyang");

			stmt = con.createStatement();
			String sql = "insert into s_subject(subjectname,classhour,gradeid) values('" + sname + "'," + hours + ","
					+ gid + ")";
			System.out.println(sql);
			int rows = stmt.executeUpdate(sql);
			System.out.println("成功增加了" + rows + "条数据");

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
